/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.excesys.exsecs.om.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.constant.CacheConstants;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.om.entity.OmODict;
import com.excesys.exsecs.om.entity.OmODictItem;
import com.excesys.exsecs.om.service.OmODictItemService;
import com.excesys.exsecs.om.service.OmODictService;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 字典表
 *
 * @author weiyj
 * @date 2023-02-24 13:53:35
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/omodict")
@Tag(name = "字典表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class OmODictController {

	private final OmODictItemService omODictItemService;

	private final OmODictService omODictService;

	/**
	 * 通过ID查询字典信息
	 * @param id ID
	 * @return 字典信息
	 */
	@GetMapping("/{id:\\d+}")
	public R<OmODict> getById(@PathVariable Long id) {
		return R.ok(omODictService.getById(id));
	}

	// /**
	// * 分页查询字典信息
	// * @param page 分页对象
	// * @return 分页对象
	// */
	// @GetMapping("/page")
	// public R<IPage<OmODict>> getDictPage(Page page, OmODict omODict) {
	// return R.ok(omODictService.page(page, Wrappers.<OmODict>lambdaQuery()
	// .like(StrUtil.isNotBlank(omODict.getSystemFlag()), OmODict::getSystemFlag,
	// omODict.getSystemFlag())));
	// }

	/**
	 * 分页查询字典信息
	 * @param page 分页对象
	 * @return 分页对象
	 */
	@GetMapping("/page")
	public R<IPage<OmODict>> getDictPage(Page page, OmODict omODict) {
		// if(BeanUtil.isNotEmpty(omODict)){
		// QueryWrapper<OmODict> qw = new QueryWrapper<>();
		// qw.lambda().eq(OmODict::getDictKey,
		// omODict.getDictKey()).eq(OmODict::getSystemFlag, omODict.getSystemFlag());
		// return R.ok(omODictService.page(page, qw));
		// }else{
		// return R.ok(omODictService.page(page, Wrappers.query(omODict)));
		// }
		return R.ok(omODictService.page(page, Wrappers.query(omODict)));

	}

	/**
	 * 通过字典类型查找字典
	 * @param key 类型
	 * @return 同类型字典
	 */
	@GetMapping("/key/{key}")
	@Cacheable(value = CacheConstants.DICT_DETAILS, key = "#key")
	public R<List<OmODictItem>> getDictByKey(@PathVariable String key) {
		return R.ok(omODictItemService.list(Wrappers.<OmODictItem>query().lambda().eq(OmODictItem::getDictKey, key)));
	}

	/**
	 * 添加字典
	 * @param omODict 字典信息
	 * @return success、false
	 */
	@SysLog("添加字典")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('om_dict_add')")
	public R<Boolean> save(@Valid @RequestBody OmODict omODict) {
		return R.ok(omODictService.save(omODict));
	}

	/**
	 * 删除字典，并且清除字典缓存
	 * @param id ID
	 * @return R
	 */
	@SysLog("删除字典")
	@DeleteMapping("/{id:\\d+}")
	@PreAuthorize("@pms.hasPermission('om_dict_del')")
	public R removeById(@PathVariable Long id) {
		omODictService.removeDict(id);
		return R.ok();
	}

	/**
	 * 修改字典
	 * @param omODict 字典信息
	 * @return success/false
	 */
	@PutMapping
	@SysLog("修改字典")
	@PreAuthorize("@pms.hasPermission('om_dict_edit')")
	public R updateById(@Valid @RequestBody OmODict omODict) {
		omODictService.updateDict(omODict);
		return R.ok();
	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param omODictItem 字典项
	 * @return
	 */
	@GetMapping("/item/page")
	public R<IPage<OmODictItem>> getSysDictItemPage(Page page, OmODictItem omODictItem) {
		return R.ok(omODictItemService.page(page, Wrappers.query(omODictItem)));
	}

	/**
	 * 查询字典項
	 * @param omODictItem 字典项
	 * @return
	 */
	@GetMapping("/item/select")
	public R<List<OmODictItem>> getSysDictItemPage(OmODictItem omODictItem) {
		return R.ok(omODictItemService.list(Wrappers.query(omODictItem)));
	}

	/**
	 * 通过id查询字典项
	 * @param id id
	 * @return R
	 */
	@GetMapping("/item/{id:\\d+}")
	public R<OmODictItem> getDictItemById(@PathVariable("id") Long id) {
		return R.ok(omODictItemService.getById(id));
	}

	/**
	 * 新增字典项
	 * @param omODictItem 字典项
	 * @return R
	 */
	@SysLog("新增字典项")
	@PostMapping("/item")
	@CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true)
	public R<Boolean> save(@RequestBody OmODictItem omODictItem) {
		return R.ok(omODictItemService.save(omODictItem));
	}

	/**
	 * 修改字典项
	 * @param omODictItem 字典项
	 * @return R
	 */
	@SysLog("修改字典项")
	@PutMapping("/item")
	public R updateById(@RequestBody OmODictItem omODictItem) {
		omODictItemService.updateDictItem(omODictItem);
		return R.ok();
	}

	/**
	 * 通过id删除字典项
	 * @param id id
	 * @return R
	 */
	@SysLog("删除字典项")
	@DeleteMapping("/item/{id:\\d+}")
	public R removeDictItemById(@PathVariable Long id) {
		omODictItemService.removeDictItem(id);
		return R.ok();
	}

	@SysLog("清除字典缓存")
	@DeleteMapping("/cache")
	@PreAuthorize("@pms.hasPermission('om_dict_del')")
	public R clearDictCache() {
		omODictService.clearDictCache();
		return R.ok();
	}

}
